package com.dailywork.basepro.thread.concurrent;

import java.util.Timer;
import java.util.TimerTask;

/**
 * 定时器任务 所有任务都是由同一个同一个线程调用，所有任务都是串行的，同一时间只有一个任务执行
 *
 * @author think
 * @since 2019/09/27
 */
public class SelfTimerTaskOne extends TimerTask {

    private String jobName = "";

    SelfTimerTaskOne(String jobName) {
        super();
        this.jobName = jobName;
    }

    @Override
    public void run() {
        System.out.println("execute " + jobName);
    }

    public static void main(String[] args) {
        long delay = 1000;
        long period = 1000;
        Timer timer = new Timer();
        timer.schedule(new SelfTimerTaskOne("job1"), delay, period);
        timer.schedule(new SelfTimerTaskOne("job2"), delay, period);

        delay = 2000;
        period = 2000;
        timer.schedule(new SelfTimerTaskOne("job1"), delay, period);
        timer.schedule(new SelfTimerTaskOne("job2"), delay, period);
    }
}
